package com.weiqiao.mall.service.impl;

import com.google.gson.Gson;
import com.mingsoft.util.StringUtil;
import com.weiqiao.mall.domain.MarketActiveEntity;
import com.weiqiao.mall.domain.MarketActivelogEntity;
import com.weiqiao.mall.domain.ShelfEntity;
import com.weiqiao.mall.repository.IMarketActiveRepository;
import com.weiqiao.mall.service.IMarketActiveService;
import com.weiqiao.utils.ArithUtil;
import com.weiqiao.utils.DateUtil;
import org.bson.conversions.Bson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @author chenyudong
 * @date 2019/5/8
 * @desc
 */
@Service("MarketActiveServiceImpl")
public class MarketActiveServiceImpl extends BaseWeiXinServiceImpl implements IMarketActiveService {


    @Autowired
    @Qualifier("MarketActiveRepositoryImpl")
    private IMarketActiveRepository iMarketActiveDao;


    @Override
    public List<MarketActiveEntity> findMarketActiveList(Bson bson, int companyId) {
        List<String> list = iMarketActiveDao.findMarketActiveList(bson, companyId);
        List<MarketActiveEntity> mList = new ArrayList<>();
        for (String str : list) {
            long currentDate = System.currentTimeMillis();//当前时间
            MarketActiveEntity entity = new Gson().fromJson(str, MarketActiveEntity.class);
            entity.setCurrentData(String.valueOf(currentDate));
            entity.setCreate_time(DateUtil.timestampToString(entity.getCreate_time()));
            //设置活动状态
            this.setMarketActiveEntityState(entity);
            //库存百分比  页面显示用
            if (entity.getInventory() > 0) {
                entity.setPercentage(ArithUtil.div(entity.getInventory(), entity.getInventory_total(), 2) * 100);
            } else {
                entity.setPercentage(100);
            }
            mList.add(entity);
        }
        return mList;
    }

    @Override
    public List<MarketActiveEntity> findMarketActiveList(Bson bson, Bson sort, int limit, int companyId) {
        List<String> list = iMarketActiveDao.findMarketActiveList(bson, sort, limit, companyId);
        List<MarketActiveEntity> mList = new ArrayList<>();
        for (String str : list) {
            long currentDate = System.currentTimeMillis();//当前时间
            MarketActiveEntity entity = new Gson().fromJson(str, MarketActiveEntity.class);
            entity.setCurrentData(String.valueOf(currentDate));
            entity.setCreate_time(DateUtil.timestampToString(entity.getCreate_time()));
            //设置活动状态
            this.setMarketActiveEntityState(entity);
            //库存百分比  页面显示用
            if (entity.getInventory() > 0) {
                entity.setPercentage(ArithUtil.div(entity.getInventory(), entity.getInventory_total(), 2) * 100);
            } else {
                entity.setPercentage(0);
            }
            mList.add(entity);
        }
        return mList;
    }

    @Override
    public MarketActiveEntity findOneMarketActive(Bson bson, int companyId) {
        String str = iMarketActiveDao.findOneMarketActive(bson, companyId);
        MarketActiveEntity entity = new MarketActiveEntity();
        if (!StringUtil.isBlank(str)) {
            entity = new Gson().fromJson(str, MarketActiveEntity.class);
            setMarketActiveEntityState(entity);
        }
        return entity;
    }

    @Override
    public List<MarketActivelogEntity> findMarketActiveLogList(Bson filter, int companyId) {
        List<String> strList = iMarketActiveDao.findMarketActiveLogList(filter, companyId);
        List<MarketActivelogEntity> activeLogList = new ArrayList<>();
        Gson gson = new Gson();
        for (String str : strList) {
            MarketActivelogEntity entity = gson.fromJson(str, MarketActivelogEntity.class);
            activeLogList.add(entity);
        }
        return activeLogList;
    }

    private void setMarketActiveEntityState(MarketActiveEntity entity) {
        //1判断当前时间是否大于开始时间,先判读是否被手动失效
        long currentDate = System.currentTimeMillis();
        if (entity.getActive_type() == 0 && (StringUtil.isBlank(entity.getStart_time()) || StringUtil.isBlank(entity.getEnd_time()))) {
            entity.setCurrentData(String.valueOf(currentDate));
            return;
        }
        long startTime = Long.parseLong(entity.getStart_time());
        long endTime = Long.parseLong(entity.getEnd_time());
        if (entity.getActive_state() != 2) {
            if (currentDate < startTime) {
                entity.setActive_state(0);//未开始
            } else if (currentDate >= startTime && currentDate <= endTime) {
                entity.setActive_state(1);//已开始
            } else if (currentDate > endTime) {
                entity.setActive_state(2);//已结束
            }
        }
        entity.setCurrentData(String.valueOf(currentDate));
    }

    private ShelfEntity setPlayNum(ShelfEntity shelf, long current_time) {
        long time_start = System.currentTimeMillis();
        long today_begin = DateUtil.getToday(time_start);
        shelf.setCurrent_time(String.valueOf(System.currentTimeMillis()));
        long ten = today_begin + (10 * 60 * 60 * 1000);
        long twelve = today_begin + (12 * 60 * 60 * 1000);
        long fifteenth = today_begin + (15 * 60 * 60 * 1000);
        long eighteen = today_begin + (18 * 60 * 60 * 1000);
        long twenty = today_begin + (20 * 60 * 60 * 1000);
        long twenty_two = today_begin + (22 * 60 * 60 * 1000);
        long today_end = today_begin + (24 * 60 * 60 * 1000 - 1000);
        long ten_tomorrow = ten + (24 * 60 * 60 * 1000);
        if (ten <= current_time && current_time < twelve) {
            shelf.setPlay_num(1);
            shelf.setEnd_time(String.valueOf(twelve));
        } else if (twelve <= current_time && current_time < fifteenth) {
            shelf.setPlay_num(2);
            shelf.setEnd_time(String.valueOf(fifteenth));
        } else if (fifteenth <= current_time && current_time < eighteen) {
            shelf.setPlay_num(3);
            shelf.setEnd_time(String.valueOf(eighteen));
        } else if (eighteen <= current_time && current_time < twenty) {
            shelf.setPlay_num(4);
            shelf.setEnd_time(String.valueOf(twenty));
        } else if (twenty <= current_time && current_time < today_end) {
            shelf.setPlay_num(5);
            shelf.setEnd_time(String.valueOf(twenty_two));
        } else if (current_time < ten) {
            shelf.setPlay_num(0);
            shelf.setEnd_time(String.valueOf(ten));
        } else if (current_time > today_end) {
            shelf.setPlay_num(0);
            shelf.setEnd_time(String.valueOf(ten_tomorrow));
        }
        return shelf;
    }
}
